package com.wangcanxuan.www.util;


import com.wangcanxuan.www.util.pool.PooledConnection;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.wangcanxuan.www.util.constant.PooledConstants.MAX_CONNECTIONS;
import static com.wangcanxuan.www.util.constant.ValidateConstant.*;

/**
 * 参数校验工具类
 *
 * @author HHLJ
 * @date 2022/04/07
 */
public class ValidateUtil {


    public static boolean isNotEmail(String email) {
        return notMatch(VALIDATE_EMAIL, email);
    }

    public static boolean isNotPassword(String password) {
        return notMatch(VALIDATE_PASSWORD, password);
    }

    public static boolean isNotMobile(String mobile) {
        return notMatch(VALIDATE_MOBILE, mobile);
    }

    public static boolean isNotAge (String age) {
        return notMatch(VALIDATE_AGE, age);
    }

    public static boolean strIsNullOrEmpty(String str) {
        return str == null || "".equals(str);
    }

    public static boolean isNull(Object object) { return object == null;}

    public static boolean isNotNull(Object object) {return object != null;}

    public static boolean judgeListMoreSize(Collection<?> list, int size) {
        return list.size() > size;
    }

    public static boolean judgeListEqualSize(Collection<?>  list,int size) {
        return list.size() == size;
    }

    public static boolean judgeListLessSize(Collection<?>  list,int size){
        return list.size() < size;
    }

    public static boolean listSizeNotZero(Collection<?>  list){
        return judgeListMoreSize(list,0);
    }

    /**
     * 获得集合中非空元素的数量大小
     *
     * @param list 列表
     * @return int
     */
    public static int getNotNullSize(List<PooledConnection> list) {
        int count = 0;
        for (PooledConnection object : list) {
            if (object != null) {
                count++;
            }
        }
        return count;
    }


    /**
     * 正则不匹配
     *
     * @param regex 正则表达式字符串
     * @param str   要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean notMatch(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return !matcher.matches();
    }

    private ValidateUtil() {
    }


}
